package com.wanmait.employment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.wanmait.employment.aop.LogInfo;
import com.wanmait.employment.entity.Admin;
import com.wanmait.employment.mapper.AdminMapper;
import com.wanmait.employment.service.AdminService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class AdminServiceImpl extends ServiceImpl<AdminMapper,Admin> implements AdminService {
    @Resource
    private AdminMapper adminMapper;


    @Override
    public PageInfo<Admin> findAll(String name, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Admin> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("name", name).or().like("admin_name",name);
        List<Admin> adminList =adminMapper.selectList(queryWrapper);
        PageInfo<Admin> adminPageInfo =new PageInfo<>(adminList);
        return adminPageInfo;
    }

    @LogInfo("更新管理员")
    @Override
    public Integer update(Admin admin) {
        return adminMapper.updateById(admin);
    }

    @LogInfo("删除管理员")
    @Override
    public Integer delete(Integer id) {
        return adminMapper.deleteById(id);
    }

    @LogInfo("添加管理员")
    @Override
    public Integer insert(Admin admin) {
        return adminMapper.insert(admin);
    }

    @Override
    public Admin login(Admin admin) {
        if (StringUtils.isBlank(admin.getAdminName())||StringUtils.isBlank(admin.getAdminPass()))
        {
            return null;
        }
        LambdaQueryWrapper<Admin> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getAdminName,admin.getAdminName());
        queryWrapper.eq(Admin::getAdminPass,admin.getAdminPass());
        return adminMapper.selectOne(queryWrapper);
    }
}
